package com.titar.robot.ui.forgotpsd;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.titar.robot.R;
import com.titar.robot.base.BaseActivity;
import com.titar.robot.utils.TntUtil;
import com.titar.robot.utils.ToastUtils;
import com.titar.robot.widgets.CountdownTextView;
import com.titar.robot.widgets.DeleteEditText;
import com.titar.robot.widgets.TntToolbar;

public class ForgotPsdActivity extends BaseActivity<ForgotPsdContact.Presenter> implements View.OnClickListener, ForgotPsdContact.View {

    DeleteEditText mEtPhone;
    DeleteEditText mEtPassword;
    DeleteEditText mEtPasswordComfirm;
    EditText mEtCardCode;
    CountdownTextView mTvSMS;
    Button mBtnFindPassword;
    TntToolbar toolbar;

    @Override
    protected void onInitWidget(Bundle savedInstanceState) {
        mEtPhone = findViewById(R.id.et_phone);
        mEtPassword = findViewById(R.id.et_psw);
        mEtPasswordComfirm = findViewById(R.id.et_psw_comfirm);
        mEtCardCode = findViewById(R.id.et_cardCode);
        mTvSMS = findViewById(R.id.tv_SMS);
        mBtnFindPassword = findViewById(R.id.btn_find_password);

        toolbar = findViewById(R.id.toolbar);
        toolbar.getTitle().setText(R.string.find_password);

        mPresenter = new ForgotPsdPresenter().attachView(this);
        getLifecycle().addObserver(mPresenter);
    }

    @Override
    protected void onInitListener() {

        mTvSMS.setOnClickListener(this);
        mBtnFindPassword.setOnClickListener(this);
    }

    @Override
    public int getContentViewId() {
        return R.layout.layout_forgotpsd_activity;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_SMS:
                checkVerifyCode();
                break;
            case R.id.btn_find_password:
                findPassword();
                break;
        }
    }

    private void checkVerifyCode() {
        String phone = mEtPhone.getText().toString().trim();
        String password = mEtPassword.getText().toString();
        String passwordComfirm = mEtPasswordComfirm.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            mTvSMS.reset();
            ToastUtils.showToast("请输入正确的手机号");
        } else if (!TntUtil.isNewLigalPsw(password)) {
            mTvSMS.reset();
            ToastUtils.showToast(getString(R.string.password_format_error_longnum));
        } else if (!TextUtils.equals(password, passwordComfirm)) {
            mTvSMS.reset();
            ToastUtils.showToast(getString(R.string.two_psw_inconsistency));
        } else {
            mPresenter.getVerifyCode(phone);
        }
    }

    private void findPassword() {

        String phone = mEtPhone.getText().toString().trim();
        String password = mEtPassword.getText().toString().trim();
        String verifyCode = mEtCardCode.getText().toString().trim();
        if (phone == null) {
            //Snackbar.make(mBtnFindPassword, "请输入正确的手机号", Snackbar.LENGTH_SHORT).show();
            ToastUtils.showToast("请输入正确的手机号");
        } else if (!TntUtil.isNewLigalPsw(password)) {
            //Snackbar.make(mBtnFindPassword, getString(R.string.password_format_hint), Snackbar.LENGTH_SHORT).show();
            ToastUtils.showToast(getString(R.string.password_format_error_longnum));
        } else if (!TntUtil.isVerifyCode(verifyCode)) {
            //Snackbar.make(mBtnFindPassword, "请输入4位短信验证码", Snackbar.LENGTH_SHORT).show();
            ToastUtils.showToast("请输入4位短信验证码");
        } else {
            mPresenter.findPassword(phone, password, verifyCode);
        }
    }

    @Override
    public void onFindPassWordSucess() {
        String phone = mEtPhone.getText().toString().trim();
        String password = mEtPassword.getText().toString().trim();
        Intent intent = new Intent();
        intent.putExtra("phone", phone);
        intent.putExtra("password", password);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onGetVerifyCodeSucess() {

    }
}
